/*
 * Copyright 2008 the original author Ang Gao.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * EditorPane.java 2008-11-10
 */
package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.editorpane;

import ie.ucc.cs.anggao.finalproject.client.support.css.CSSManager;
import ie.ucc.cs.anggao.finalproject.client.support.widget.BlogServerDialogBox;
import ie.ucc.cs.anggao.finalproject.client.ui.BasePane;
import ie.ucc.cs.anggao.finalproject.client.ui.event.QueryEvent;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.editorpane.RichTextToolbar.RichTextToolbar;
import ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.editorpane.menubar.menuBar;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TreeImages;
import com.google.gwt.user.client.ui.Widget;

/**
 * A {@link BasePane} showing a Rich client Editor
 * 
 * @author GaoAng
 * 
 */
public class EditorPane extends BasePane {

	private RichTextArea richtextarea;
	private RichTextToolbar richtexttoolbar;
	private menuBar menubar;
	private String query;
	private HorizontalPanel toppanel;
	private TextBox titletext;

	public interface Images extends TreeImages {

		AbstractImagePrototype adobecs3();

		AbstractImagePrototype Blog();

	}

	public EditorPane() {
		richtextarea = new RichTextArea();
		richtextarea.ensureDebugId("cwRichText-area");
		richtextarea.setSize("50em", "30em");

		richtexttoolbar = new RichTextToolbar(richtextarea);
		richtexttoolbar.ensureDebugId("cwRichText-toolbar");
		richtexttoolbar.setWidth("100%");

		menubar = new menuBar(richtextarea);

		toppanel = new HorizontalPanel();
		toppanel.setSpacing(10);
		titletext = new TextBox();
		titletext.setSize("300", "20");
		Label titlelabel = new Label("Title:  ");

		// Get the images
		Images images = (Images) GWT.create(Images.class);

		PushButton publishbutton = new PushButton(images.Blog().createImage(),
				new ClickListener() {
					public void onClick(Widget sender) {
						DialogBox dialogbox = new BlogServerDialogBox(
								EditorPane.this).createDialog();
						dialogbox.setAnimationEnabled(true);
						dialogbox.center();
						dialogbox.show();
					}
				});

		PushButton pdfbutton = new PushButton(images.adobecs3().createImage(),
				new ClickListener() {
					public void onClick(Widget sender) {
						Window.open("/GeneratePDF?", "newWindowName",
								"window features.");

					}
				});

		toppanel.add(titlelabel);
		toppanel.add(titletext);
		toppanel.add(publishbutton);
		toppanel.add(pdfbutton);
		toppanel.setSpacing(10);

		Grid grid = new Grid(4, 2);
		grid.setStyleName(CSSManager.RichTextEditor);
		grid.setWidget(0, 0, new Label("     "));
		grid.setWidget(1, 0, new Label("     "));
		grid.setWidget(2, 0, new Label("     "));
		grid.setWidget(3, 0, new Label("     "));
		grid.setWidget(0, 1, toppanel);
		grid.setWidget(1, 1, richtexttoolbar);
		grid.setWidget(2, 1, menubar);
		grid.setWidget(3, 1, richtextarea);

		query = "";

		Timer timer = new Timer() {

			public void run() {

				if (!query.equals(richtextarea.getText())) {
					query = richtextarea.getText();
					if (!query.isEmpty())
						fireEvent(new QueryEvent(EditorPane.this, query,
								richtextarea.getHTML()));
				}

			}
		};

		timer.scheduleRepeating(3000);

		initWidget(grid);

	}

	public String getAreaText() {
//		System.out.println("------------HTML-------" + richtextarea.getHTML());
		return richtextarea.getHTML();

	}

	public String getTitleText() {
		return titletext.getText();
	}

}
